
export default class RandomUtil {

    /** 随机minNum到maxNum的数字 （包含maxNum） */
    public static nextInt(minNum: number, maxNum: number) {
        return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
    }

    public static nextNumber(minNum: number, maxNum: number) {
        return Math.random() * (maxNum - minNum) + minNum;
    }

    public static nextSign() {
        let temp = Math.random();
        if (temp < 0.5) {
            return 1
        }
        return -1;
    }

    public static nextBoolean() {
        let temp = Math.random();
        if (temp < 0.5) {
            return true
        }
        return false;
    }

    public static randomArr(nowArr: Array<any>, needNum: number, saveChangeArr: boolean = false) {
        let tempArr: Array<any>
        if (saveChangeArr) {
            tempArr = nowArr
        } else {
            tempArr = nowArr.concat()
        }
        let resultArr: Array<any> = [];
        for (let index = 0; index < needNum; index++) {
            if (tempArr.length <= 0) {
                break;
            }
            let randomIndex: number = RandomUtil.nextInt(0, tempArr.length - 1);
            resultArr.push(tempArr.splice(randomIndex, 1)[0]);
        }
        return resultArr;
    }

    public static randomItem(nowArr: Array<any>, saveChangeArr: boolean = false) {
        return this.randomArr(nowArr, 1, saveChangeArr)[0];
    }

    public static randomIndexByPercent(rateList: Array<number> = [100, 100, 100]) {
        let tempList: Array<number> = [];
        let totalTemp: number = 0;
        for (let i = 0; i < rateList.length; i++) {
            totalTemp += rateList[i];
            tempList[i] = totalTemp;
        }
        let randomTemp: number = this.nextInt(0, totalTemp);
        let resultIndex: number = tempList.length - 1;
        for (let i = 0; i < tempList.length; i++) {
            if (randomTemp <= tempList[i]) {
                resultIndex = i;
                break;
            }
        }
        return resultIndex;
    }



}
